package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class BibliotecaApp {
    private Menu menu;
    private Library library;
    private PrintStream printer;
    private BufferedReader reader;

    public BibliotecaApp(PrintStream printer, BufferedReader reader) {
        this.printer = printer;
        this.reader = reader;
        this.menu = new Menu(printer, reader);
        this.library = new Library();
    }

    public static void main(String[] args) throws IOException {
        BibliotecaApp biblioteca = new BibliotecaApp(System.out, new BufferedReader(new InputStreamReader(System.in)));
        biblioteca.run();
    }

    public void run() throws IOException {
        welcomeMessage();
        showMenu();
        int choice = menu.selectMenuOption(menu.getSize());
        executeChoice(choice);

    }

    private void welcomeMessage() {
        printer.println("Welcome!");
    }

    private void executeChoice(int choice) throws IOException {
        switch(choice) {
            case 1:  checkMembership(); break;
            case 2:  checkReserved(); break;
            case 3:  showBooks(); break;
            case 4:  showMovies(); break;
            default: break;
        }

    }

    private void showMovies() {
        printer.printf("%-23s %-4s %-8s %-5s\n", "Name", "Year", "Director", "Rating");
        for (Movie movie : library.getMovies()) {
            printer.printf("%-23s %-4s %-8s %-5s\n", movie.getName(), movie.getYear(), movie.getDirector(), movie.getRating());
        }
    }

    private void checkMembership() {
        printer.println("Please talk to a Librarian. Thank you.");
    }

    private void checkReserved() throws IOException {
        String name = reader.readLine();
        printer.println(library.bookExists(name));
    }

    private void showBooks() {
        for (Book book : library.getBooks()) {
            printer.println(book.getName());
        }
    }

    private void showMenu() {
        for (String menuItem : menu.getMenu()) {
            printer.println(menuItem);
        }
    }
}
